package me.zhengjie.modules.quartz.task;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.persistence.*;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * 自动生成订单任务
 * @author xiahongbo
 * @date 2022-07-20
 */
@Slf4j
@Async
@Component
public class MakeOrderTask {

    @Autowired
    private EntityManager entityManager;

    @Transactional
    public void makeOrder() {
        // 当前时间
        String datetime = DateUtil.format(DateUtil.date(), DatePattern.NORM_DATETIME_MS_PATTERN);
        // 当前日志
        String date = DateUtil.format(DateUtil.date(), DatePattern.NORM_DATE_PATTERN);
        log.info("自动生成订单任务 {}" + datetime);
        /**
         * StoredProcedureQuery 创建存储过程仓库
         * createStoredProcedureQuery（name） name 时过程名
         * registerStoredProcedureParameter表示存储过程中注册的参数
         * ParameterMode 参数
         */
        StoredProcedureQuery storedProcedureQuery = this.entityManager.createNamedStoredProcedureQuery("makeOrder");
        /*
        storedProcedureQuery.registerStoredProcedureParameter("IR_BZ", String.class, ParameterMode.IN);
        storedProcedureQuery.registerStoredProcedureParameter("IR_CURRENT_DATE", String.class, ParameterMode.IN);
        storedProcedureQuery.registerStoredProcedureParameter("OR_APPCODE", String.class, ParameterMode.OUT);
        storedProcedureQuery.registerStoredProcedureParameter("OR_MESSAGE", String.class, ParameterMode.OUT);
*/
        /**
         * 设置传入参数值
         */
        storedProcedureQuery.setParameter("IR_BZ","定时生成订单");
        storedProcedureQuery.setParameter("IR_CURRENT_DATE",date);
        /**
         * 执行
         */
        storedProcedureQuery.execute();
        int code = (int) storedProcedureQuery.getOutputParameterValue("OR_APPCODE");
        String node = (String) storedProcedureQuery.getOutputParameterValue("OR_MESSAGE");

        /**
         * 获取结果出参
         *//*
        int code = (int) storedProcedureQuery.getOutputParameterValue("OR_APPCODE");
        String node = (String) storedProcedureQuery.getOutputParameterValue("OR_MESSAGE");
*/
        log.info("过程执行结果:code:{}，node:{}",code,node);
    }


    @Transactional
    public void makeOrder2() {
        List<Object> infoviewList=new ArrayList<>();
        // 当前时间
        String datetime = DateUtil.format(DateUtil.date(), DatePattern.NORM_DATETIME_MS_PATTERN);
        // 当前日志
        String date = DateUtil.format(DateUtil.date(), DatePattern.NORM_DATE_PATTERN);
        log.info("自动生成订单任务 {} " + datetime);
        Query query = this.entityManager.createNativeQuery("begin prc_make_order(:inParam1, :inParam2, :inParam3, :inParam4); end;");

        int code = 0;
        String msg = "";
        query.setParameter("inParam1", "定时生成订单");
        query.setParameter("inParam2", date);
        query.setParameter("inParam3", code);
        query.setParameter("inParam4", msg);
        int i = query.executeUpdate();

        log.info("过程执行结果:code:{}，node:{}", code, msg);
    }
}
